package com.starrocks.controller;

import com.starrocks.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池监控 Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/thread-pool")
public class ThreadPoolController {
    
    @Autowired
    @Qualifier("fullSyncExecutor")
    private ThreadPoolTaskExecutor fullSyncExecutor;
    
    /**
     * 获取线程池状态
     */
    @GetMapping("/status")
    public Result<Map<String, Object>> getStatus() {
        try {
            ThreadPoolExecutor executor = fullSyncExecutor.getThreadPoolExecutor();
            
            Map<String, Object> status = new HashMap<>();
            status.put("corePoolSize", executor.getCorePoolSize());
            status.put("maxPoolSize", executor.getMaximumPoolSize());
            status.put("activeThreads", executor.getActiveCount());
            status.put("poolSize", executor.getPoolSize());
            status.put("queueSize", executor.getQueue().size());
            status.put("queueCapacity", fullSyncExecutor.getQueueCapacity());
            status.put("queueRemainingCapacity", executor.getQueue().remainingCapacity());
            status.put("completedTasks", executor.getCompletedTaskCount());
            status.put("totalTasks", executor.getTaskCount());
            status.put("largestPoolSize", executor.getLargestPoolSize());
            
            // 计算利用率
            int poolUtilization = executor.getPoolSize() > 0 
                ? (int) (executor.getActiveCount() * 100.0 / executor.getPoolSize())
                : 0;
            status.put("poolUtilization", poolUtilization);
            
            int queueUtilization = fullSyncExecutor.getQueueCapacity() > 0
                ? (int) (executor.getQueue().size() * 100.0 / fullSyncExecutor.getQueueCapacity())
                : 0;
            status.put("queueUtilization", queueUtilization);
            
            // 状态描述
            String statusDesc;
            if (executor.getActiveCount() == 0) {
                statusDesc = "空闲";
            } else if (executor.getActiveCount() < executor.getCorePoolSize()) {
                statusDesc = "正常";
            } else if (executor.getActiveCount() < executor.getMaximumPoolSize()) {
                statusDesc = "繁忙";
            } else {
                statusDesc = "满载";
            }
            status.put("statusDesc", statusDesc);
            
            return Result.success(status);
        } catch (Exception e) {
            log.error("获取线程池状态失败", e);
            return Result.error("获取状态失败：" + e.getMessage());
        }
    }
}

